<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%!
	//定义数据库驱动程序
	public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
	//数据库连接地址
	public static final String DBURL = "jdbc:mysql://localhost:3306/lab";
	public static final String DBUSER = "root";
	public static final String DBPASS = "123";
%>
<%
	Connection conn = null;                      //声明数据库连接对象
	PreparedStatement pstmt = null;              //声明数据库操作
	ResultSet rs = null;                         //声明数据库结果集
	boolean flag = false;                        //定义标志位
	String name = request.getParameter("name");;                          //接收用户名
	String p1 = request.getParameter("password1");
	String p2 = request.getParameter("password2");
%>
<%  //JDBC操作抛出异常
try {
	if(p1.equals(p2)){
		Class.forName(DBDRIVER);                     //加载驱动程序
		conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);//取得数据库连接
		//编写sql语句，验证id密码
		String sql = "INSERT INTO user(username,gender,age,email,password) VALUES(?,?,?,?,?)";
		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1,request.getParameter("name"));
		pstmt.setString(2,request.getParameter("sex"));
		pstmt.setString(3,request.getParameter("age"));
		pstmt.setString(4,request.getParameter("mail"));
		pstmt.setString(5,request.getParameter("password1"));
		pstmt.executeUpdate();   //执行查询

		flag = true;
	}else{
		%>
		<jsp:forward page="error.jsp">
		<jsp:param name="error" value="password wrong"/>
		</jsp:forward>
		<% 
	}
}catch(Exception e){
	System.out.println(e);
}finally{
	try{
		rs.close();
		pstmt.close();
		conn.close();
	}catch(Exception e){}
}
%>
<%
	if(flag){                      //登陆成功，跳转到成功页面
%>
	<jsp:forward page="index.jsp">
	<jsp:param name="uname" value="<%=name %>"/>
	</jsp:forward>
<%
	}else{                       //登陆失败
%>
	<jsp:forward page="error.jsp"/>
	<%
	}
	%>
</body>
</html>